// Package sys
// @Link  https://github.com/bufanyun/hotgo
// @Copyright  Copyright (c) 2024 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License  https://github.com/bufanyun/hotgo/blob/master/LICENSE
// @AutoGenerate Version 2.13.1
package sys

import (
	"context"
	"fmt"
	"hotgo/internal/dao"
	"hotgo/internal/library/hgorm/handler"
	"hotgo/internal/model/input/form"
	"hotgo/internal/model/input/sysin"
	"hotgo/internal/service"
	"hotgo/utility/convert"
	"hotgo/utility/excel"
	"hotgo/utility/tradeDate"

	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/util/gconv"
)

type sSysStockBlock struct{}

func NewSysStockBlock() *sSysStockBlock {
	return &sSysStockBlock{}
}

func init() {
	service.RegisterSysStockBlock(NewSysStockBlock())
}

// Model 板块轮动ORM模型
func (s *sSysStockBlock) Model(ctx context.Context, option ...*handler.Option) *gdb.Model {
	return handler.Model(dao.StockBlock.Ctx(ctx), option...)
}

// List 获取板块轮动列表
func (s *sSysStockBlock) List(ctx context.Context, in *sysin.StockBlockListInp) (list []*sysin.StockBlockListModel, totalCount int, err error) {
	mod := s.Model(ctx)

	// 查询id
	if in.Id > 0 {
		mod = mod.Where(dao.StockBlock.Columns().Id, in.Id)
	}

	// 查询code
	if in.Code != "" {
		mod = mod.Where(dao.StockBlock.Columns().Code, in.Code)
	}

	// 查询名称
	if in.Name != "" {
		mod = mod.WhereLike(dao.StockBlock.Columns().Name, "%"+in.Name+"%")
	}

	// 查询日期
	if in.Date != "" {
		startDay := tradeDate.GetStartTradeDay(in.Date, 8)
		mod = mod.WhereBetween(dao.StockBlock.Columns().Date, startDay, in.Date)
	}

	totalCount, err = mod.Clone().Count()
	if err != nil {
		err = gerror.Wrap(err, "获取板块轮动数据行失败，请稍后重试！")
		return
	}

	if totalCount == 0 {
		return
	}

	if err = mod.Fields(sysin.StockBlockListModel{}).OrderDesc(dao.StockBlock.Columns().Strength).Scan(&list); err != nil {
		err = gerror.Wrap(err, "获取板块轮动列表失败，请稍后重试！")
		return
	}
	return
}

// Export 导出板块轮动
func (s *sSysStockBlock) Export(ctx context.Context, in *sysin.StockBlockListInp) (err error) {
	list, totalCount, err := s.List(ctx, in)
	if err != nil {
		return
	}

	// 字段的排序是依据tags的字段顺序，如果你不想使用默认的排序方式，可以直接定义 tags = []string{"字段名称", "字段名称2", ...}
	tags, err := convert.GetEntityDescTags(sysin.StockBlockExportModel{})
	if err != nil {
		return
	}

	var (
		fileName  = "导出板块轮动-" + gctx.CtxId(ctx) + ".xlsx"
		sheetName = fmt.Sprintf("索引条件共%v行,共%v页,当前导出是第%v页,本页共%v行", totalCount, form.CalPageCount(totalCount, in.PerPage), in.Page, len(list))
		exports   []sysin.StockBlockExportModel
	)

	if err = gconv.Scan(list, &exports); err != nil {
		return
	}

	err = excel.ExportByStructs(ctx, tags, exports, fileName, sheetName)
	return
}

// Edit 修改/新增板块轮动
func (s *sSysStockBlock) Edit(ctx context.Context, in *sysin.StockBlockEditInp) (err error) {
	// 修改
	var res *sysin.StockBlockViewModel
	if err = s.Model(ctx).Where(dao.StockBlock.Columns().Code, in.Code).
		Where(dao.StockBlock.Columns().Date, in.Date).Scan(&res); err != nil {
		err = gerror.Wrap(err, "获取板块轮动信息，请稍后重试！")
		return
	} else {
		if res == nil {
			if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).
				Fields(sysin.StockBlockInsertFields{}).
				Data(in).Insert(); err != nil {
				err = gerror.Wrap(err, "新增板块轮动失败，请稍后重试！")
			}
		} else {
			if _, err = s.Model(ctx).
				Fields(sysin.StockBlockUpdateFields{}).
				Where(dao.StockBlock.Columns().Code, in.Code).
				Where(dao.StockBlock.Columns().Date, in.Date).
				Data(in).Update(); err != nil {
				err = gerror.Wrap(err, "修改板块轮动失败，请稍后重试！")
			}
			return
		}
	}
	return
}

// Delete 删除板块轮动
func (s *sSysStockBlock) Delete(ctx context.Context, in *sysin.StockBlockDeleteInp) (err error) {
	if _, err = s.Model(ctx).WherePri(in.Id).Delete(); err != nil {
		err = gerror.Wrap(err, "删除板块轮动失败，请稍后重试！")
		return
	}
	return
}

// View 获取板块轮动指定信息
func (s *sSysStockBlock) View(ctx context.Context, in *sysin.StockBlockViewInp) (res *sysin.StockBlockViewModel, err error) {
	if err = s.Model(ctx).WherePri(in.Id).Scan(&res); err != nil {
		err = gerror.Wrap(err, "获取板块轮动信息，请稍后重试！")
		return
	}
	return
}
